﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class per_documentary_me : MpageBase
{
    protected Model.UsersAccount user_account = null;
    protected Model.CMember CMember = null;
    protected List<Model.LotteryChipped> buyInfo = null;
    protected int m = ReadRequest.RequestInt("m");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
            bindData();
        }
    }
    protected void bindData()
    {
        Bind();
        PagedDataSource pds = new PagedDataSource();
        string sql = "";
        int type = int.Parse(this.typeslddl.SelectedValue);

        if (type > 0)
        {
            sql = " UserID=" + loginUserInfo.Id + " and LotteryType=" + type;
        }
        else
        {

            sql = " UserID=" + loginUserInfo.Id;
        }

        int times = int.Parse(this.timesl.SelectedValue.ToString());
        if (times == 0)
        {
            sql += " and datediff(day,[AddTime],getdate())=0 ";
        }
        else if (times == 1)
        {
            sql += " and datediff(week,[AddTime],getdate())=0 ";
        }
        else if (times == 2)
        {
            sql += " and datediff(month,[AddTime],getdate())=0";
        }
        else if (times == 3)
        {
            sql += " and datediff(month,[AddTime],getdate())<=3 ";
        }
        else if (times == 4)
        {
            sql += " and datediff(month,[AddTime],getdate())<=6 ";
        }
        //是否中奖
        if (CheckBox1.Checked)
        {
            sql += " and LotteryChipped.IsWin=1";
        }
        //是否开奖
        if (CheckBox2.Checked)
        {
            sql += " and LotteryChipped.Flag=0";
        }
        int totalCount = 0;
        buyInfo = BLL.LotteryChipped.GetPageListBysql2(this.AspNetPager1.CurrentPageIndex - 1, this.AspNetPager1.PageSize, sql, out totalCount);
        if (buyInfo != null && buyInfo.Count > 0)
        {
            AspNetPager1.Visible = true;
            AspNetPager1.RecordCount = totalCount;
            pds.AllowPaging = true;
            pds.PageSize = AspNetPager1.PageSize;
            pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;

        }
        else
        {
            AspNetPager1.Visible = false;
        }
    }
    private void Bind()
    {
        if (loginUserInfo != null && loginUserInfo.Id > 0)
        {
            user_account = BLL.UsersAccount.GetModelByuser_id(loginUserInfo.Id);
            CMember = BLL.CMember.GetModel(loginUserInfo.Id);
            if (user_account == null)
            {
                BLL.Session.Remove(BLL.CMember.LoginUserSessionKey);
                Response.Redirect("/default.aspx");
            }
        }
        else
        {
            Response.Redirect("/default.aspx");
        }
    }
    public string select(object userid)
    {
        Model.CMember cm = null;
        cm = BLL.CMember.GetModel(long.Parse(userid.ToString()));
        return cm != null ? cm.nickname : "暂无姓名";
    }
    public decimal winMoney(object lotteryid)
    {
        //中奖金额/跟单的总额*每人的跟单金额
        Model.LotteryChipped chipped = BLL.LotteryChipped.GetModel(long.Parse(lotteryid.ToString()));
        DataTable ds = BLL.LotteryChippedUser.GetPageListBysql("select * from LotteryChippedUser where LotteryChippedID=" + int.Parse(lotteryid.ToString()));

        decimal money = 0;//所有跟单人的金额
        decimal usermoney = 0;//该用户中奖的金额
        if (ds.Rows.Count > 0)
        {
            for (int i = 0; i < ds.Rows.Count; i++)
            {
                money += decimal.Parse(ds.Rows[i]["MySub"].ToString());
            }
            DataTable userds = BLL.LotteryChippedUser.GetPageListBysql("select * from LotteryChippedUser where LotteryChippedID=" + int.Parse(lotteryid.ToString()) + " and UserID=" + loginUserInfo.Id);

            decimal userGen = 0;//该用户跟单金额
            for (int i = 0; i < ds.Rows.Count; i++)
            {
                userGen += decimal.Parse(ds.Rows[i]["MySub"].ToString());
            }
            if (chipped != null && chipped.WinMoney > 0)
            {
                usermoney = chipped.WinMoney / money * userGen;
            }
        }

        return usermoney;
    }
    public decimal getGendaMoeny(object lotteryid)
    {
        DataTable ds = BLL.LotteryChippedUser.GetPageListBysql("select * from LotteryChippedUser where LotteryChippedID=" + int.Parse(lotteryid.ToString()) + " and UserID=" + loginUserInfo.Id);

        decimal money = 0;
        for (int i = 0; i < ds.Rows.Count; i++)
        {
            money += decimal.Parse(ds.Rows[i]["MySub"].ToString());
        }
        return money;
    }
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        Bind();
        bindData();
    }
    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
        Bind();
        bindData();
    }
    protected void typeslddl_SelectedIndexChanged(object sender, EventArgs e)
    {
        Bind();
        bindData();
    }
    protected void timesl_SelectedIndexChanged(object sender, EventArgs e)
    {
        Bind();
        bindData();
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        Bind();
        bindData();
    }
}